import {PicInfoService} from "./PicInfoService";

var menuInfoDaoObj = require('../dao/MenuInfoDao.js');

var response = require('./../util/response.js');

var menuInfoDao = new menuInfoDaoObj.MenuInfoDao();


/**
 * { function_description }
 *
 * @class      UserService (name)
 */
export function MenuInfoService() {
    console.info('into MenuInfoService.js...');
}

MenuInfoService.prototype.constructor = MenuInfoService;


/**
 * { 新增菜单 }
 *
 * @param      {<type>}    params  The parameters
 * @return     {response}  { description_of_the_return_value }
 */
MenuInfoService.prototype.saveMenuInfo = function(params) {
    for (let i in params) {
        if (params[i] == null || undefined || '') {
            this.errorMsg = '参数不齐全!';
            console.error(this.errorMsg)
            return this.errorMsg;
        }
    }
    var params2 = {};
    /*此处赋值要对应SQL语句中的字段顺序,不然会段与值之间发生错位*/
    params2.menuName = params.menuName;//菜单名称
    params2.parentId = params.parentId;//菜单对应父id,如果没有则为0
    params2.createTime = params.createTime; //默认值
    try {
        menuInfoDao.saveMenuInfo(params2);
    } catch (error) {
        console.error(error);
        return new response.response(error.errno, error.code, 0);
    }

    return new response.response(200, 'OK', 1);
};
/**
 * 修改菜单信息
 * @param picInfo
 * @returns {response}
 */
MenuInfoService.prototype.updateMenu = (menuInfo) => {
    try {
        menuInfoDao.updatesById(menuInfo);
    } catch (err) {
        return new response.response(505, err, '');
    }
    return new response.response(200, '', null);
}
/**
 * 获取菜单列表
 *
 * @return     {<type>}  { description_of_the_return_value }
 */
MenuInfoService.prototype.findAllMenu = function() {
    return menuInfoDao.getMenuInfoList();
};

/**
 * { 删除图片信息 }
 *
 * @param      {<type>}    params  The parameters
 * @return     {response}  { description_of_the_return_value }
 */
MenuInfoService.prototype.deleteMenuInfos = function(params) {
    var result = 0;
    try {
        if ((params == null || undefined || '')||params.length<=0) {
            this.errorMsg = 'id不能为空!';
            return this.errorMsg;
        }
        var menuList = [];
        console.log('ddddddddddddd',params)
        for(var i =0;i<params.length;i++){
            if(params[i].type==0){
                menuList.push(params[i].id);
            }
        }

        //删除菜单
        if(menuList.length>0){
            var picParam = { column: 'id', value: menuList };
            console.log('删除菜单',picParam);
            result = menuInfoDao.deleteByIds(picParam);//先删除本身
            picParam.column = 'parent_id';
            console.log('删除子菜单',picParam);
            result = menuInfoDao.deleteByIds(picParam);//在删除子菜单
        }
    } catch (error) {
        return new response.response(error.errno, error.code, 0);
    }
    return new response.response(200, 'OK', result);
}


